package com.chen.lifepic.DataSource;

import android.database.sqlite.SQLiteDatabase;

public class DownloadedFileDB {
	private SQLiteDatabase mSqLiteDatabase = null;

	private static final String DB_PATH = "data/data/com.chen.lifepic/";

	private static final String DB_NAME = "lifePic.db";

	private static final String LOCAL_DATA = "local_data";

	private static final String AUTO_ID = "id";

	private static final String NAME = "name";

	private static final String THUMBNAIL_PATH = "thumbnial_path";

	private static final String ORIGINAL_PATH = "original_path";

	private static final String THUMBNAIL_URL = "thumbnail_url";

	private static final String ORIGINAL_URL = "original_url";

	private static final String INSERT_TIME = "insert_time";

	public DownloadedFileDB() {
		initDataBase();
	}

	private void initDataBase() {
		try {
			mSqLiteDatabase = SQLiteDatabase.openDatabase(
				DB_PATH + DB_NAME, null, SQLiteDatabase.CREATE_IF_NECESSARY);
			initTable();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private boolean initTable() {
		if (null == mSqLiteDatabase)
			return false;

		String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS " + LOCAL_DATA + "( "
				+ AUTO_ID + " INTEGER PRIMARY KEY, " + NAME + " CHAR, "
				+ THUMBNAIL_PATH + " CHAR, " + ORIGINAL_PATH + " CHAR,"
				+ THUMBNAIL_URL + " CHAR," + ORIGINAL_URL + " CHAR,"
				+ INSERT_TIME + "CHAR)";
		mSqLiteDatabase.execSQL(CREATE_TABLE);

		return true;
	}

	// public void insert(MediaInfo item) {
	// try {
	// if (null == mSqLiteDatabase || null == item)
	// return;
	// ContentValues values = new ContentValues();
	// values.put(LAP_TIME, item.lapTime);
	// values.put(TOTAL_TIME, item.totalTime);
	// values.put(COLOR, item.color);
	// values.put(INSERT_TIME, item.insertTime);
	// mSqLiteDatabase.insert(HISTORY_TABLE, null, values);
	//
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// }
	//
	// public void updateColor(MediaInfo item) {
	// try {
	// if (null == mSqLiteDatabase || null == item)
	// return;
	//
	// ContentValues values = new ContentValues();
	// String where = INSERT_TIME + "=? and " + TOTAL_TIME + "=? and "
	// + LAP_TIME + "=?";
	// String[] whereValues = { Long.toString(item.insertTime),
	// Long.toString(item.totalTime), Long.toString(item.lapTime) };
	// values.put(COLOR, item.color);
	// int number = mSqLiteDatabase.update(
	// HISTORY_TABLE, values, where, whereValues);
	// // Utils.logDebug("test", "updateColor = "
	// // + ((number == 1) ? "true" : "false"));
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// }
	//
	// public Cursor query() {
	// try {
	// return mSqLiteDatabase.query(
	// HISTORY_TABLE, null, null, null, null, null, null);
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// return null;
	// }
	//
	// public void delete(long insertTime) {
	// try {
	// String whereClause = INSERT_TIME + "= ?";
	// String[] whereArgs = { Long.toString(insertTime) };
	// mSqLiteDatabase.delete(HISTORY_TABLE, whereClause, whereArgs);
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// }
	//
	// public static LapItem getDataByCursor(Cursor cursor) {
	// if (null == cursor)
	// return null;
	// LapItem item = new LapItem();
	// item.id = cursor.getInt(cursor.getColumnIndex(AUTO_ID));
	// item.lapTime = cursor.getLong(cursor.getColumnIndex(LAP_TIME));
	// item.totalTime = cursor.getLong(cursor.getColumnIndex(TOTAL_TIME));
	// item.color = cursor.getInt(cursor.getColumnIndex(COLOR));
	// item.insertTime = cursor.getLong(cursor.getColumnIndex(INSERT_TIME));
	// return item;
	// }

	public void close() {
		if (null != mSqLiteDatabase) {
			mSqLiteDatabase.close();
			mSqLiteDatabase = null;
		}
	}

}
